package cqrtplm.service.impl;

import com.hustcad.plm.pdm.enumeration.model.vo.EnumerationItemHandleVO;
import com.hustcad.plm.pdm.enumeration.service.TyplmEnumerationItemService;
import com.ty.basic.entity.IdentifierEntity;
import cqrtplm.entity.RatoCpytDO;
import cqrtplm.entity.RtPrimaryPurpose;
import cqrtplm.mapper.RatoCpytMapper;
import cqrtplm.service.RatoCpytService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class RatoCpytServiceImpl implements RatoCpytService {
    @Value("${enum.primary.purpose}")
    private String primaryPurpose;
    @Autowired
    private TyplmEnumerationItemService typlmEnumerationItemService;

    private final RatoCpytMapper ratoCpytMapper;

    @Override
    public int create(RatoCpytDO record) {
        return ratoCpytMapper.insert(record);
    }

    @Override
    public int delete(String id) {
        return ratoCpytMapper.deleteById(id);
    }

    @Override
    public int update(RatoCpytDO record) {
        return ratoCpytMapper.update(record);
    }

    @Override
    public RatoCpytDO getById(String id) {
        return ratoCpytMapper.selectById(id);
    }

    @Override
    public List<RatoCpytDO> query(RatoCpytDO condition) {
        return ratoCpytMapper.selectByCondition(condition);
    }

    @Override
    public List<RtPrimaryPurpose> getEnum() {
        String oidstr = ratoCpytMapper.selectPrimaryPurpose(primaryPurpose);
        IdentifierEntity identifierEntity = new IdentifierEntity();
        identifierEntity.setOidStr(oidstr);
        List<EnumerationItemHandleVO> enumVOs = typlmEnumerationItemService.queryEnumerationItemByDefOid(identifierEntity);

        List<RtPrimaryPurpose> rtPrimaryPurposes = new ArrayList<>();

        for (EnumerationItemHandleVO enumVO : enumVOs) {
            RtPrimaryPurpose rtPrimaryPurpose = new RtPrimaryPurpose();
            rtPrimaryPurpose.setOid(String.valueOf(enumVO.getOid()));
            rtPrimaryPurpose.setInternalName(enumVO.getInternalName());
            rtPrimaryPurpose.setDisplayName(enumVO.getDisplayName());
            rtPrimaryPurposes.add(rtPrimaryPurpose);
        }

        return rtPrimaryPurposes;
    }

}
